package com.story.web.utils;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.story.domain.Admin;
import com.story.domain.User;

/*
 * @WebUtils web层工具类，提供一些方法供controller使用
 * 
 * */
public class WebUtils {
	public static final int HTTP_STATUS_SESSION_EXPIRE = 401;

	// 用户ID生成器
	public static String makeId() {
		return UUID.randomUUID().toString();
	}

	// 用户密码加密器
	public static void entryPassword(User user) {
		String algorithmName = "md5";
		String salt = user.getUsername() + System.currentTimeMillis();
		SimpleHash sh = new SimpleHash(algorithmName, user.getPassword(), salt,
				10);
		user.setSalt(salt);
		user.setPassword(sh.toHex());
	}

	// 管理员密码加密器
	public static void entryPassword(Admin admin) {
		String algorithmName = "sha1";
		String salt = admin.getUsername() + System.currentTimeMillis()
				+ Math.random();
		SimpleHash sh = new SimpleHash(algorithmName, admin.getPassword(),
				salt, 100);
		admin.setSalt(salt);
		admin.setPassword(sh.toBase64());
	}

	// 普通用户初始化
	public static User init(HttpServletRequest request, User user) {
		user.setUid(makeId());
		entryPassword(user);
		user.setRegisteTime(new Date(System.currentTimeMillis()));
		String registeIp = request.getRemoteAddr();
		user.setRegisteIp(registeIp);
		user.setActiveStatus(false);
		user.setAccountStatus(true);
		return user;
	}

	// 超级管理员初始化
	public static Admin init(HttpServletRequest request, Admin admin) {
		admin.setAid(makeId());
		entryPassword(admin);
		admin.setCreateTime(new Date(System.currentTimeMillis()));
		String createIp = request.getRemoteAddr();
		admin.setCreateIp(createIp);
		;
		admin.setAccountStatus(true);
		admin.setLevel(10);
		return admin;
	}

	// 判断是否ajax请求
	public static boolean isAjax(HttpServletRequest request) {
		return (request.getHeader("X-Requested-With") != null && "XMLHttpRequest"
				.equals(request.getHeader("X-Requested-With").toString()));
	}

	public static Map<String, Object> createMessage(String key, Object value) {
		Map<String, Object> map = new HashedMap();
		map.put(key, value);
		return map;
	}
	//获取用户真实IP
	public static String getCustomerRealIp(HttpServletRequest request) {
		if (request.getHeader("x-forwarded-for") == null) {
			return request.getRemoteAddr();
		}
		return request.getHeader("x-forwarded-for");
	}
	//利用cookie获取用户上次访问时间
	public static Date getLastLoginTimeFromCookie(HttpServletRequest request){
		Cookie cookies[] = request.getCookies();
		for(int i=0;cookies!=null&&i<cookies.length;i++){
			if(cookies[i].getName().equals("lastLoginTime")){
				Long cookieValue = Long.parseLong(cookies[i].getValue());
				Date date = new Date(cookieValue);
				return date;
			}
		}
		return null;
	}
	//利用cookie记录用户上次访问时间
	public static void writeLoginTimeToCookie(HttpServletResponse response){
		Cookie cookie = new Cookie("lastLoginTime",System.currentTimeMillis()+"");
		cookie.setMaxAge(3600);
		cookie.setPath("/story");
		response.addCookie(cookie);
	}
	
	public static Session getSession(Subject subject){
		Session session = subject.getSession();
		if(session == null){
			session = subject.getSession(true);
		}
		return session;
	}
	
	public static boolean isCurrentSuject(Admin admin,String id){
		if(admin.getAid().equals(id)){
			return true;
		}
		return false;
	}
}	
